<template>
  <div class="system-container">
    <vab-query-form>
      <el-form :inline="true" :model="queryForm" @submit.native.prevent>
        <el-form-item class="pr8">
          <el-select
            v-model="queryForm.machine"
            clearable
            filterable
            placeholder="机器"
            style="width: 120px"
          >
            <el-option
              v-for="(item, index) in machines"
              :key="index"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item class="pr8">
          <el-input
            v-model="queryForm.filename"
            clearable
            placeholder="文件名称"
          />
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="queryForm.time"
            end-placeholder="结束日期"
            range-separator=" ~ "
            start-placeholder="文件开始日期"
            style="width: 250px"
            type="daterange"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item>
          <el-checkbox
            v-model="queryForm.parseable"
            border
            class="ml12"
            :false-label="0"
            :true-label="1"
          >
            未解析
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="queryData">
            查询
          </el-button>
          <el-dropdown @command="handleCommand">
            <el-button icon="el-icon-document" type="success">
              解析机器文件
              <i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                v-for="(item, index) in machines"
                :key="index"
                :command="item"
              >
                {{ item.name }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-form-item>
      </el-form>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      @row-dblclick="handleDetail"
    >
      <el-table-column align="center" label="ID" prop="id" width="80" />
      <el-table-column align="center" label="机器" prop="machine" width="80" />
      <el-table-column
        align="center"
        label="文件名称"
        prop="filename"
        width="140"
      />
      <el-table-column
        align="center"
        label="生成时间"
        prop="time"
        width="140"
      />
      <el-table-column
        align="center"
        label="入库时间"
        prop="created_at"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column
        align="center"
        label="是否解析"
        prop="content_parse"
        width="100"
      >
        <template #default="scope">
          <el-tag v-if="scope.row.content_parse" type="success">已解析</el-tag>
          <el-tag v-else type="danger">未解析</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="140">
        <template #default="scope">
          <el-link
            class="mr12"
            icon="el-icon-edit"
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
          >
            修改
          </el-link>
          <el-link
            icon="el-icon-document"
            size="mini"
            type="success"
            @click="parseOne(scope.row)"
          >
            解析
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :layout="layout"
      :pagesize="queryForm.pagesize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import {
    getAll as getList,
    sync as syncMachine,
    parseOne,
  } from '@/api/inspectMachine'
  import { getAll as getMachines } from '@/api/machine'
  import Edit from './components/edit'
  import { removeEmptyField } from '@/utils'
  export default {
    name: 'InspectMachine',
    components: {
      Edit,
    },
    props: {
      activeName: {
        type: String,
        default() {
          return 'fourth'
        },
      },
    },
    data() {
      return {
        list: [],
        machines: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        listLoading: false,
        selectRows: '',
        queryForm: {
          machine: '',
          time: '',
          filename: '',
          parseable: '',
          page: 1,
          pagesize: 10,
        },
        dialogVisible: false,
        bloodMatch: null,
        title: '',
      }
    },
    created() {
      this.getMachines()
      this.fetchData()
    },
    methods: {
      async getMachines() {
        const {
          data: { data },
        } = await getMachines({ pagesize: 100, order: 'name asc' })
        this.machines = data
      },
      async syncMachine(machine_id, machine_name) {
        const { msg } = await syncMachine(machine_id)
        this.$baseMessage(msg, 'success', 'vab-hey-message-success')
        this.queryForm.machine = machine_name
        this.fetchData()
      },
      handleCommand(machine) {
        this.syncMachine(machine.id, machine.name)
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      async parseOne(row) {
        const { data, msg } = await parseOne(row.id, [])
        console.log(data)
        this.$baseMessage(msg, 'success', 'vab-hey-message-success')
        this.fetchData()
      },
      print(row) {
        console.log(row)
      },
      queryData() {
        delete this.queryForm.page
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(removeEmptyField(this.queryForm))
        this.list = data.data
        this.total = data.total
        this.listLoading = false
      },
      handleDetail(row) {
        console.log(row)
      },
      handleRefresh() {
        this.fetchData()
      },
      handleSizeChange(val) {
        this.queryForm.pagesize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
    },
  }
</script>

<style lang="scss" scoped>
  .pr8 {
    padding-right: 8px;
  }
</style>
